<?php
include '../config/config.php';
include '../lib/produk.php';
include '../lib/SimpleImage.php';

$config=new config();
$config->connect();

session_start();
if (!isset($_SESSION['MM_Username'])){
	$togo="../login.php";
	header ("Location:$togo");
}

/*$Produk_Dao=new Produk_Dao();
$produk=$Produk_Dao->get_idmember($_SESSION['MM_Id']);*/

if ($_POST){

	$Produk_Dao=new Produk_Dao();
	$produk = new Produk();
	$produk->nama_produk=$_POST['nama_produk'];
	$produk->jumlah=$_POST['jumlah'];
	$produk->fasilitas=$_POST['fasilitas'];
	$produk->harga=$_POST['harga'];
	$produk->foto_produk=$_FILES['foto_produk']['name'];
	$produk->id=$_SESSION['MM_Id'];
	$produk->id_tipe=$_POST['tipe'];
	$list_produk[] = $produk;
	$type = pathinfo($_FILES['foto_produk']['name']);

  if ($_POST['nama_produk']!="" && $_POST['jumlah']!="" && $_POST['fasilitas']!="" && $_POST['harga']!=""){

		if($type['extension'] == "jpg" || $type['extension'] == "png"){
			$fileName = $_FILES['foto_produk']['name'];
			$fileSize = $_FILES['foto_produk']['size'];
			$fileError = $_FILES['foto_produk']['error'];
			
			if($fileSize > 0 || $fileError == 0){
				if(file_exists("images/produk/".$fileName)){
					unlink("images/produk/".$fileName);
				}
				$image = new SimpleImage();
				$image->load($_FILES['foto_produk']['tmp_name']);
				$image->resize(150,150);
				$image->save("images/produk/".$fileName);
				$move = file_exists("images/produk/".$fileName);
				if($move){
					$Produk_Dao->add($produk);
					//header("Location:daftar_promo.php");
					echo "<script>alert('Data berhasil disimpan');</script>";
				}else{
					echo "<script>alert('Data gagal disimpan');</script>";
				}
			}
		}
	} else {
		echo "Isikan data dengan lengkap terlebih dahulu!";
	}
	
}

?>
<?php include("header_member.php") ?>
<?php include("menu_kiri_member.php") ?>

<div id="body">
<div class="topbox">
                  Tambah Produk:
                  <br>
				  <br>
				  <form method="post" action="" enctype="multipart/form-data">
				  <input type="hidden" name="ubah_produk">
                  <table border=1 cellspacing=0 cellpadding=3>
				   <tr>
					<td>Nama Produk*</td>
					<td>:</td>
					<td><input type="text" name="nama_produk" id="nama_produk" value = ""></td>
				  </tr>
				  <tr>
					<td>Tipe*</td>
					<td>:</td>
					<td><select name="tipe" id="tipe">
					<option value="1">Single</option>
					<option value="2">Double</option>
					<option value="3">Twin</option>
					<option value="4">Double-double</option>
					</select></td>
				  </tr>
				  <tr>
					<td>Jumlah*</td>
					<td>:</td>
					<td><input type="text" name="jumlah" id="jumlah" value = ""></td>
				  </tr>
				  <tr>
					<td>Fasilitas*</td>
					<td>:</td>
					<td><input type="text" name="fasilitas" id="fasilitas" value = ""></td>
				  </tr>
				  <tr>
					<td>Harga*</td>
					<td>:</td>
					<td><input type="text" name="harga" id="harga" value = ""></td>
				  </tr>
				  <tr>
					<td>Keterangan Produk*</td>
					<td>:</td>
					<td><input type="file" name="foto_produk" id="foto_produk" accept="image/*"></td>
				  </tr>
				  </table>
				  <br>
				  <input type="submit" value="Simpan"><input type="button" value=" Batal " onClick="window.location.href='daftar_produk.php'">
				  </form>
                  <br>                
</div> 
<div class="topbox">
* Data harus diisi.
</div>            
</div>
          
         <div class="clear"></div>
         <?php include("../footer.php") ?>
		